package com.chima.action;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.chima.domain.PageRequestBean;
import com.chima.domain.PageResponseBean;
import com.chima.domain.PostCompetent;
import com.chima.service.PostCompetentService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import flexjson.JSONSerializer;

public class PostCompetentAction extends ActionSupport implements ModelDriven<PostCompetent>{
	PostCompetent postCompetent=new PostCompetent();
	public PostCompetent getModel() {
		
		return postCompetent;
	}
	private int start;// 开始页码
	private int limit; //每页显示
	private int page;//页数
	private  PostCompetentService postCompetentService;
	private List<PostCompetent> postCompetents;
	private boolean success=false ;
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public PostCompetentService getPostCompetentService() {
		return postCompetentService;
	}
	public void setPostCompetentService(PostCompetentService postCompetentService) {
		this.postCompetentService = postCompetentService;
	}
	public boolean isSuccess() {
		return success;
	}
	public void setSuccess(boolean success) {
		this.success = success;
	}
	public String  ListpostCompetents() throws Exception{
	    PageRequestBean pageRequestBean = new PageRequestBean();
		pageRequestBean.setStart(start);
		pageRequestBean.setLimit(limit);
		pageRequestBean.setPage(page);
		PageResponseBean pageResponseBean =postCompetentService.findPostCompetent(pageRequestBean);
		postCompetents=pageResponseBean.getRows();
        int total=0;
        total =pageResponseBean.getTotal();
		// 输出json内容到客户端
        JSONSerializer jsonSerializer = new JSONSerializer();
        jsonSerializer.exclude("*.class");
        String resultJson1 = jsonSerializer.deepSerialize(postCompetents);  
        String resultJson2 =jsonSerializer.serialize(total);
      
        String jsonString = "{total:"+resultJson2.toString()+",postCompetents:"+resultJson1.toString()+"}";
       
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
		return NONE;
  }
	public String addCompetent() throws Exception{
		PostCompetent p =new PostCompetent();
		p.setGrade(postCompetent.getGrade());
		p.setCoeff(postCompetent.getCoeff());
		p.setWorkyear(postCompetent.getWorkyear());
		postCompetentService.savePostCompetent(p);
		
		this.success=true;
		JSONSerializer jsonSerializer = new JSONSerializer();
		String resultJson =jsonSerializer.serialize(success);
		String jsonString = "{success:"+resultJson.toString()+"}";
		System.out.println(jsonString);
		ServletActionContext.getResponse().setContentType("text;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
		return NONE;
		
	}
	public void editPostCompetent() throws Exception{
	    
		try {
			postCompetentService.updatePostCompetent(postCompetent);
		} catch (Exception e) {	
			e.printStackTrace();
		}	
		this.success=true;
		JSONSerializer jsonSerializer = new JSONSerializer();
		String resultJson =jsonSerializer.serialize(success);
		String jsonString = "{success:"+resultJson.toString()+"}";
		System.out.println(jsonString);
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
		}
	public String deletePostCompetent(){
		 postCompetentService.deletePostCompetent(postCompetent.getId());
			return NONE;
		}

}
